package com.wing.setting.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.setting.model.entity.ProcessInfo;
import com.wing.setting.model.request.ProcessInfoForm;
import com.wing.setting.model.response.ProcessInfoVO;
import com.wing.setting.service.ProcessInfoService;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 */
@Validated
@Api(value = "Admin-工序管理", tags = "#/setting/processInfo")
@RestController("processInfoController")
@RequestMapping("setting/admin/processInfo")
@RequiredArgsConstructor
public class ProcessInfoController {

    private final ProcessInfoService processInfoService;

    @ApiOperation(value = "查询所有工序", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            , @ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            , @ApiImplicitParam(name = "sort", value = "排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            , @ApiImplicitParam(name = "order", value = "排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<ProcessInfoVO> list(
            @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit
            , @RequestParam(defaultValue = "create_time") String sort
            , @RequestParam(defaultValue = "asc") String order
            , @RequestParam(value = "productTypeId", required = false) Long productTypeId
            , @RequestParam(value = "type", required = false) Long type
            , @RequestParam(value = "code", required = false) String code
            , @RequestParam(value = "roomInfoId", required = false) Long roomInfoId
    ) {

        Page<ProcessInfoVO> pg = new Page<>(page, limit);
        Map map = new HashMap();
        map.put("order", order);
        map.put("productTypeId", productTypeId);
        map.put("type", type);
        //查询已经存在的工序
        IPage<ProcessInfoVO> list = processInfoService.pageList4app(pg, map);
        if (type != null) {
            if (1 == type) {
                Map newMap = new HashMap();
                newMap.put("order", order);
                //查询所有工序
                Page<ProcessInfoVO> pg2 = new Page<>(page, limit);
                IPage<ProcessInfoVO> allList = processInfoService.pageList4app(pg2, newMap);
                for (ProcessInfoVO all : allList.getRecords()) {
                    for (ProcessInfoVO listRecord : list.getRecords()) {
                        if (all.getId().equals(listRecord.getId())) {
                            all.setElect(true);
                        }
                    }
                }
                return new PageResult<>(allList.getRecords(), allList.getTotal());
            }
        }

        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @ApiOperation(value = "添加工序", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@Validated(ProcessInfoForm.Add.class) ProcessInfoForm processInfoForm) {
        Map map = new HashMap();
        map.put("nodeNames", processInfoForm.getNodeNames());
        List list = processInfoService.list4app(map);
        if (list.size() > 0) {
            return JsonResult.fail("添加失败,工序名称重复");
        }
        if (processInfoService.add(processInfoForm) > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }


    @ApiOperation(value = "修改工序", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@Validated(ProcessInfoForm.Update.class) ProcessInfoForm processInfoForm) {
        ProcessInfo processInfo = processInfoService.getById(processInfoForm.getId());
        if (processInfo == null) {
            return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(ProcessInfo.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(processInfoForm, processInfo, copyOptions);
        if (processInfoService.updateById(processInfo)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }

    @ApiOperation(value = "修改工序", notes = "修改")
    @PutMapping("/updateProductType")
    public JsonResult updateProductType(@Validated(ProcessInfoForm.Update.class) ProcessInfoForm processInfoForm) {

        return JsonResult.success("修改成功！");
    }


    @ApiOperation(value = "删除工序", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "processInfoId", value = "id", required = true, dataType = "Integer", paramType = "path")
    })
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long processInfoId) {
        if (processInfoService.removeById(processInfoId)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }

    @ApiOperation(value = "根据id查询工序", notes = "查询")
    @GetMapping("/selectProcessInfoById")
    public JsonResult<ProcessInfo> selectProcessInfoById(@RequestParam(value = "id") Long id) {
        ProcessInfo processInfo = processInfoService.getById(id);
        return JsonResult.success(processInfo);

    }

    @ApiOperation(value = "查询所有工序", notes = "查询")
    @GetMapping("/getAllProcessInfo")
    public JsonResult<List<ProcessInfo>> getAllProcessInfo() {
        QueryWrapper<ProcessInfo> wrapper = new QueryWrapper<>();
        wrapper.select("id", "node_names");
        List<ProcessInfo> list = processInfoService.list(wrapper);
        return JsonResult.success(list);

    }
}
